import User from '@/store/user'


interface pageModel{
    link_type : string,
    link_url : string,
    element_name : string,
    is_login : string,

}

export function  pushPage(param:pageModel){
    // if(param.is_login === '1' && User.isNotLogin()){
    //     alert('请登录')
    //     return;
    // }
    param.link_url = 'http://www.baidu.com'
    // console.log('跳转的链接:' + param.link_url)
    if(param.link_type === '1'){ // h5页面
        push(param.link_url, 'H5', false)
    } else if(param.link_type === '2'){ // 小程序

    } 
}

export function pushNative(url:string, needAuth: boolean){
     push(url, 'Native',needAuth)
}

export function pushH5(url:string, needAuth: boolean){
    push(url, 'H5',needAuth)
}


export function push(url:string, type:string, needAuth:boolean){
    if( !url || url.length < 1){
        console.log('跳转地址为空，请检查跳转配置')
        return
    }

    if(needAuth === true && User.isNotLogin()){

    }

//   console.log('跳转页面:'+url)
    if(type === 'H5'){
      
    }else{
        var pages = getCurrentPages()
        if(pages.length >= 10){
            uni.redirectTo({
                url
            })
        }else{
            uni.navigateTo({
                url
            })
        }
       
    }
}

export function back(){
    let pages = getCurrentPages();
    if(pages.length <= 1){
       relaunchHome()
    }else{
        uni.navigateBack()

    }
}

export function backPageCount(count:number){
    uni.navigateBack({
        delta: count
    })
 }
 

export function gotoLogin(){

    // // #ifdef MP-WEIXIN
    // console.log('微信登录');
    //     uni.login({
    //         provider:'weixin',
    //         success:function (loginRes){
    //             console.log('微信小程序授权返回:' + JSON.stringify(loginRes))
    //         }
    //     })
    // // #endif
    pushNative('/pages/index/author',false)
}

export function getUserInfo(){
    uni.getUserInfo({
        provider: 'weixin',
        success: function (infoRes) {
          console.log('用户昵称为：' + JSON.stringify(infoRes.userInfo));
        }
      });
}

export function switchTab(page:string){
    uni.switchTab({ url: page })
 }

export function redirectToPage(page:string){
    uni.redirectTo({ url: page })
 }

export function relaunchHome(){
   uni.reLaunch({ url: '/pages/index/index' })
}

export function relaunch(page:string){
    uni.reLaunch({ url: page })
 }
 


export function show404(){
    uni.reLaunch({ url: '/pages/index/index' })
 }